package com.hp.docker_base.util;

import com.hp.docker_base.bean.*;
import com.hp.docker_base.mapper.AnorectalMapper;
import com.hp.docker_base.mapper.StomachMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class ReadUtil {

    @Autowired
    StomachMapper stomachMapper;

    @Autowired
    AnorectalMapper anorectalMapper;

    public ArrayList<ArrayList<Double>> testData() throws IOException {
        FileReader fr_reference = new FileReader("E:\\我爱学习\\胃病系统\\data\\lin_100.txt");
        String line;

        BufferedReader br2 = new BufferedReader(fr_reference);
        ArrayList<ArrayList<Double>> Reference = new ArrayList<>();
        while ((line = br2.readLine()) != null) {
            String[] temp = line.split("\t");
            ArrayList<Double> Temp = new ArrayList<>();
            for (int i = 0; i < temp.length; i++) {
                Temp.add(Double.parseDouble(temp[i]));
            }
            Reference.add(Temp);
        }
        br2.close();

        return Reference;
    }

    public void dataWrite() throws IOException {
        FileReader fr_belief = new FileReader("E:\\我爱学习\\胃病系统\\data\\belief_418.txt");
        FileReader fr_reference = new FileReader("E:\\我爱学习\\胃病系统\\data\\rule30_418.txt");
        BufferedReader br1 = new BufferedReader(fr_belief);

        ArrayList<ArrayList<Double>> Belief = new ArrayList<>();
        String line;
        while ((line = br1.readLine()) != null) {
            String[] temp = line.split("\t");
            ArrayList<Double> Temp = new ArrayList<>();
            for (int i = 0; i < temp.length; i++) {
                Temp.add(Double.parseDouble(temp[i]));
            }
            Belief.add(Temp);
        }
        br1.close();

        BufferedReader br2 = new BufferedReader(fr_reference);
        ArrayList<ArrayList<Double>> Reference = new ArrayList<>();
        while ((line = br2.readLine()) != null) {
            String[] temp = line.split("\t");
            ArrayList<Double> Temp = new ArrayList<>();
            for (int i = 0; i < temp.length; i++) {
                Temp.add(Double.parseDouble(temp[i]));
            }
            Reference.add(Temp);
        }
        br2.close();

        ArrayList<StomachRule> stomachRules = new ArrayList<>();
        for (int i = 0; i < Reference.size(); i++) {
            StomachRule stomachRule = new StomachRule();
            stomachRule.setRule(i + 1);
            stomachRule.setId01(Reference.get(i).get(0));
            stomachRule.setId02(Reference.get(i).get(1));
            stomachRule.setId03(Reference.get(i).get(2));
            stomachRule.setId04(Reference.get(i).get(3));
            stomachRule.setId05(Reference.get(i).get(4));
            stomachRule.setId06(Reference.get(i).get(5));
            stomachRule.setId07(Reference.get(i).get(6));
            stomachRule.setId08(Reference.get(i).get(7));
            stomachRule.setId09(Reference.get(i).get(8));

            stomachRule.setSk01(Belief.get(i).get(0));
            stomachRule.setSk02(Belief.get(i).get(1));
            stomachRule.setSk03(Belief.get(i).get(2));
            stomachRule.setSk04(Belief.get(i).get(3));
            stomachRule.setSk05(Belief.get(i).get(4));
            stomachRule.setSk06(Belief.get(i).get(5));
            stomachRule.setSk07(Belief.get(i).get(6));
            stomachRule.setSk08(Belief.get(i).get(7));
            stomachRule.setSk09(Belief.get(i).get(8));
            stomachRule.setSk10(Belief.get(i).get(9));
            stomachRule.setSk11(Belief.get(i).get(10));
            stomachRule.setSk12(Belief.get(i).get(11));

            stomachRules.add(stomachRule);
        }



        //stomachMapper.InsertRule(stomachRules);

    }

    public Map Stomach_dataRead() {
        ArrayList<StomachRule> allData = stomachMapper.getAllData();
        ArrayList<ArrayList<Double>> reference = new ArrayList<>();
        ArrayList<ArrayList<Double>> belief = new ArrayList<>();

        for (int i = 0; i < allData.size(); i++) {
            ArrayList<Double> ref = new ArrayList<>();
            ArrayList<Double> blf = new ArrayList<>();
            ref.add(allData.get(i).getId01());
            ref.add(allData.get(i).getId02());
            ref.add(allData.get(i).getId03());
            ref.add(allData.get(i).getId04());
            ref.add(allData.get(i).getId05());
            ref.add(allData.get(i).getId06());
            ref.add(allData.get(i).getId07());
            ref.add(allData.get(i).getId08());
            ref.add(allData.get(i).getId09());

            blf.add(allData.get(i).getSk01());
            blf.add(allData.get(i).getSk02());
            blf.add(allData.get(i).getSk03());
            blf.add(allData.get(i).getSk04());
            blf.add(allData.get(i).getSk05());
            blf.add(allData.get(i).getSk06());
            blf.add(allData.get(i).getSk07());
            blf.add(allData.get(i).getSk08());
            blf.add(allData.get(i).getSk09());
            blf.add(allData.get(i).getSk10());
            blf.add(allData.get(i).getSk11());
            blf.add(allData.get(i).getSk12());

            reference.add(ref);
            belief.add(blf);
        }

        Map<String, ArrayList<ArrayList<Double>>> map = new HashMap<>();
        map.put("reference", reference);
        map.put("belief", belief);
        return map;
    }

    public Map Anore1_dataRead() {
        ArrayList<AnoreRule1> allData = anorectalMapper.getAllData1();
        ArrayList<ArrayList<Double>> reference = new ArrayList<>();
        ArrayList<ArrayList<Double>> belief = new ArrayList<>();

        for (int i = 0; i < allData.size(); i++) {
            ArrayList<Double> ref = new ArrayList<>();
            ArrayList<Double> blf = new ArrayList<>();
            ref.add(allData.get(i).getId01());
            ref.add(allData.get(i).getId02());
            ref.add(allData.get(i).getId03());
            ref.add(allData.get(i).getId04());
            ref.add(allData.get(i).getId05());

            blf.add(allData.get(i).getSk01());
            blf.add(allData.get(i).getSk02());

            reference.add(ref);
            belief.add(blf);
        }

        Map<String, ArrayList<ArrayList<Double>>> map = new HashMap<>();
        map.put("reference", reference);
        map.put("belief", belief);
        return map;
    }

    public Map Anore21_dataRead() {
        ArrayList<AnoreRule21> allData = anorectalMapper.getAllData21();
        ArrayList<ArrayList<Double>> reference = new ArrayList<>();
        ArrayList<ArrayList<Double>> belief = new ArrayList<>();

        for (int i = 0; i < allData.size(); i++) {
            ArrayList<Double> ref = new ArrayList<>();
            ArrayList<Double> blf = new ArrayList<>();
            ref.add(allData.get(i).getId01());
            ref.add(allData.get(i).getId02());
            ref.add(allData.get(i).getId03());
            ref.add(allData.get(i).getId04());
            ref.add(allData.get(i).getId05());
            ref.add(allData.get(i).getId06());
            ref.add(allData.get(i).getId07());
            ref.add(allData.get(i).getId08());
            ref.add(allData.get(i).getId09());
            ref.add(allData.get(i).getId10());
            ref.add(allData.get(i).getId11());
            ref.add(allData.get(i).getId12());
            ref.add(allData.get(i).getId13());
            ref.add(allData.get(i).getId14());
            ref.add(allData.get(i).getId15());
            ref.add(allData.get(i).getId16());

            blf.add(allData.get(i).getSk01());
            blf.add(allData.get(i).getSk02());

            reference.add(ref);
            belief.add(blf);
        }

        Map<String, ArrayList<ArrayList<Double>>> map = new HashMap<>();
        map.put("reference", reference);
        map.put("belief", belief);
        return map;
    }

    public Map Anore31_dataRead() {
        ArrayList<AnoreRule31> allData = anorectalMapper.getAllData31();
        ArrayList<ArrayList<Double>> reference = new ArrayList<>();
        ArrayList<ArrayList<Double>> belief = new ArrayList<>();

        for (int i = 0; i < allData.size(); i++) {
            ArrayList<Double> ref = new ArrayList<>();
            ArrayList<Double> blf = new ArrayList<>();
            ref.add(allData.get(i).getId01());
            ref.add(allData.get(i).getId02());
            ref.add(allData.get(i).getId03());
            ref.add(allData.get(i).getId04());
            ref.add(allData.get(i).getId05());
            ref.add(allData.get(i).getId06());
            ref.add(allData.get(i).getId07());
            ref.add(allData.get(i).getId08());
            ref.add(allData.get(i).getId09());
            ref.add(allData.get(i).getId10());
            ref.add(allData.get(i).getId11());

            blf.add(allData.get(i).getSk01());
            blf.add(allData.get(i).getSk02());

            reference.add(ref);
            belief.add(blf);
        }

        Map<String, ArrayList<ArrayList<Double>>> map = new HashMap<>();
        map.put("reference", reference);
        map.put("belief", belief);
        return map;
    }

    public Map Anore32_dataRead() {
        ArrayList<AnoreRule32> allData = anorectalMapper.getAllData32();
        ArrayList<ArrayList<Double>> reference = new ArrayList<>();
        ArrayList<ArrayList<Double>> belief = new ArrayList<>();

        for (int i = 0; i < allData.size(); i++) {
            ArrayList<Double> ref = new ArrayList<>();
            ArrayList<Double> blf = new ArrayList<>();
            ref.add(allData.get(i).getId01());
            ref.add(allData.get(i).getId02());
            ref.add(allData.get(i).getId03());
            ref.add(allData.get(i).getId04());
            ref.add(allData.get(i).getId05());
            ref.add(allData.get(i).getId06());
            ref.add(allData.get(i).getId07());
            ref.add(allData.get(i).getId08());
            ref.add(allData.get(i).getId09());
            ref.add(allData.get(i).getId10());
            ref.add(allData.get(i).getId11());
            ref.add(allData.get(i).getId12());
            ref.add(allData.get(i).getId13());
            ref.add(allData.get(i).getId14());

            blf.add(allData.get(i).getSk01());
            blf.add(allData.get(i).getSk02());
            blf.add(allData.get(i).getSk03());
            blf.add(allData.get(i).getSk04());
            blf.add(allData.get(i).getSk05());
            blf.add(allData.get(i).getSk06());

            reference.add(ref);
            belief.add(blf);
        }

        Map<String, ArrayList<ArrayList<Double>>> map = new HashMap<>();
        map.put("reference", reference);
        map.put("belief", belief);
        return map;
    }

}
